import React from 'react'
import PropTypes from 'prop-types'
//context 是全局的 组件里声明 所有子元素可以直接获取 context要设置type 父组件设置childContextTypes
//需要接收的需要定义contextTypes
class Sidebar extends React.Component{
    render(){
        return(
            <div>
                <p>Sidebar</p>
                <Navbar></Navbar>
            </div>
        )
    }
}
class Navbar extends React.Component{
    static contextTypes={
        user:PropTypes.string
    }
    render(){
        console.log(this.context)
        return(
            <div>{this.context.user}的Navbar</div>
        )
    }
}
class Page extends React.Component{
    static childContextTypes={
        user:PropTypes.string
    }
    constructor(props){
        super(props)
        this.state={user:'蜗牛'}
    }
    getChildContext(){
        return this.state
    }
    render(){
        const user='蜗牛'
        return(
            <div>
                <p>我是{user}</p>
                <Sidebar user={user}></Sidebar>
            </div>
        )
    }
}
export default Page